package com.yantong.service.impl;

import com.yantong.exception.BDException;
import com.yantong.mapper.MarketingSeckillMapper;
import com.yantong.mapper.MarketingSeckillSkuMapper;
import com.yantong.mapper.ProductCentreMapper;
import com.yantong.mapper.ProductMapper;
import com.yantong.pojo.ChooseProductRes;
import com.yantong.pojo.MarketingGroup;
import com.yantong.pojo.MarketingSeckill;
import com.yantong.pojo.vo.ChooseProductVo;
import com.yantong.pojo.vo.MemberCartVo;
import com.yantong.service.MarketingGroupService;
import com.yantong.service.MarketingSeckillService;
import com.yantong.service.OrderService;
import com.yantong.utils.PageUtils;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 支付服务
 *
 * @author kanghaixin
 * @date 2018/9/29.9:50
 */
@Service
public class MarketingSeckillServiceImpl implements MarketingSeckillService {

	private static Logger logger = LoggerFactory.getLogger(MarketingSeckillServiceImpl.class);

	@Autowired
	private MarketingSeckillMapper marketingSeckillMapper;

	@Autowired
	private MarketingSeckillSkuMapper marketingSeckillSkuMapper;

	//平台商品
	@Autowired
	private ProductMapper productMapper;

	//商户商品
	@Autowired
	private ProductCentreMapper productCentreMapper;
	@Autowired
	private MarketingGroupService marketingGroupService;
	@Autowired
	@Lazy
	private OrderService orderService;


	@Override
	public int changeStock(Integer total, Long activityId) {
		return marketingSeckillMapper.changeStock(total, activityId);
	}

	@Override
	public int changeSkuStockAlone(Integer total, Long activityId, Long skuId) {
		int count = marketingSeckillSkuMapper.changeStock(total, activityId, skuId);
		return count;
	}

	@Override
	public PageUtils<ChooseProductRes> chooseProductList(int offset, int limit, ChooseProductVo productVo) {
		PageUtils<ChooseProductRes> pageUtils = new PageUtils<>();
		Map<String, Object> params = new HashMap<>();
		if (productVo.getChoosePlat() == 0) {
			params.put("status", 1);
			params.put("title", productVo.getTitle());
			Long total = productMapper.chooseProductCount(params);
			if (total > 0) {
				pageUtils.setTotal(Integer.parseInt(total + ""));
				List<ChooseProductRes> list = productMapper.chooseProductList(params, new RowBounds(offset, limit));
				pageUtils.setRows(list);
			} else {
				pageUtils.setRows(new ArrayList<>());
			}

		} else if (productVo.getChoosePlat() == 1) {
			params.put("status", 1);
			params.put("title", productVo.getTitle());
			params.put("centreId", productVo.getCentreId());
			params.put("teamId", productVo.getTeamId());
			params.put("auditing", 1);
			if (productVo.getProductType() != null) {
				params.put("productType", productVo.getProductType());
			}else{
				params.put("productType", 0);
			}
			params.put("activityId", productVo.getActivityId());
			Long total = productCentreMapper.chooseProductCount(params);
			if (total > 0) {
				pageUtils.setTotal(Integer.parseInt(total + ""));
				List<ChooseProductRes> list = productCentreMapper.chooseProductList(params, new RowBounds(offset, limit));
				pageUtils.setRows(list);
			} else {
				pageUtils.setRows(new ArrayList<>());
			}
		}
		return pageUtils;
	}

	@Override
	public MarketingSeckill getMarketingSeckillByProductId(Long productId) {
		return marketingSeckillMapper.getMarketingSeckillByProductId(productId);
	}

	@Override
	public int changeSkuStock(Integer total, Long activityId, Long skuId) {
		int count = marketingSeckillSkuMapper.changeStock(total, activityId, skuId);
		changeStock(total, activityId);
		return count;
	}

	/**
	 * 校验库存和时间
	 *
	 * @param isSubimtOrgType 1: 秒杀 2：团购
	 * @param memberCartVo    提交商品信息
	 * @param sgorderId       团购预定的
	 */
	@Override
	public void checkStockAndTime(Long activityId, Integer isSubimtOrgType, MemberCartVo memberCartVo, Long sgorderId) {
		long now = System.currentTimeMillis();
		//活动结束时间
		Date endTime = null;
		//活开始时间
		Date startTime = null;
		Integer needJoinGroupNum = 0;
		if (isSubimtOrgType == 2) {
			MarketingGroup marketingGroup = marketingGroupService.getMarketingGroupById(activityId);
			if (marketingGroup == null) {
				throw new BDException("活动不存在");
			}
			endTime = marketingGroup.getEndTime();
			startTime = marketingGroup.getStartTime();
			needJoinGroupNum = marketingGroup.getNeedJoinGroupNum();
		}
		//判断是否已经停售
		if ((endTime != null) && (now > endTime.getTime())) {
			throw new BDException("活动已结束");
		}
		//判断是否开售
		if ((startTime != null) && (now < startTime.getTime())) {
			throw new BDException("活动未开始");
		}
		if (isSubimtOrgType == 2) {
			if (sgorderId != null && sgorderId > 0) {
				int spellGroupOrdCount = orderService.getSpellGroupOrdCount(sgorderId);
				if (spellGroupOrdCount >= needJoinGroupNum) {
					throw new BDException("此团以满");
				}
			}


		}


	}

}
